package com.sharemarking.wa.platform.service;

import com.sharemarking.wa.common.ResponseParams;
import com.sharemarking.wa.common.entity.OrgVerify;
import com.sharemarking.wa.common.entity.SearchHistory;
import com.sharemarking.wa.common.exception.RequestException;
import com.sharemarking.wa.common.entity.OrgWaste;
import com.sharemarking.wa.platform.dto.cms.CmsPostListDto;
import com.sharemarking.wa.platform.dto.index.*;
import com.sharemarking.wa.platform.mapper.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author dxy
 */
@Service
public class IndexService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private IndexMapper indexMapper;
    @Autowired
    private OrgWasteMapper orgWasteMapper;
    @Autowired
    private OrgVerifyMapper orgVerifyMapper;
    @Autowired
    private SearchHistoryMapper searchHistoryMapper;

    public ResponseParams<?> getList() throws Exception {
        Integer memberId = ss.getCurrentUserId();

        //判断是什么角色
        Integer role = memberMapper.getRoleByMemberId(memberId);

        IndexListDto indexListDto = new IndexListDto();

        // 获取轮播图列表
        indexListDto.setBannerList(indexMapper.getBannerList());

        //获取最新资讯列表
        indexListDto.setNewsList(indexMapper.getNewsList(System.currentTimeMillis() / 1000));

        //获取栏目列表
        indexListDto.setChannelList(indexMapper.getChannelList());

        //未阅读数
        indexListDto.setMessageNumber(indexMapper.getMessageNumber(memberId));

        //获取经营单位列表(产废端显示) role = 0
        if (role == 0) {
            List<OrgListDto> orgListDto = indexMapper.getOrgList(memberId);
            for (OrgListDto o : orgListDto) {
                o.setWasteList(indexMapper.getOrgWasteByOrgId(o.getOrgId()));
            }
            indexListDto.setOrgList(
                    orgListDto.stream().sorted(
                            Comparator.comparingInt(
                                    OrgListDto::getSize).reversed()
                    ).collect(Collectors.toList())
            );
        }
        //判断是否认证
        if (role == 0 || role == 1) {
            OrgVerify isOrgVerify = orgVerifyMapper.isOrgVerify(memberId);
            if (isOrgVerify == null) {
                indexListDto.setIsOrgVerify(3);
            } else {
                indexListDto.setIsOrgVerify(isOrgVerify.getExamState());
            }

        }

        return ResponseParams.ok(indexListDto);
    }


    public ResponseParams<?> getVisitorList() throws Exception {

        IndexListDto indexListDto = new IndexListDto();

        // 获取轮播图列表
        indexListDto.setBannerList(indexMapper.getBannerList());

        //获取最新资讯列表
        indexListDto.setNewsList(indexMapper.getNewsList(System.currentTimeMillis() / 1000));

        //获取栏目列表
        indexListDto.setChannelList(indexMapper.getChannelList());

        return ResponseParams.ok(indexListDto);
    }

    public ResponseParams<?> getOrgInfo() throws Exception {
        String orgId = request.getParameter("orgId");

        if (StringUtils.isEmpty(orgId)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("orgId"));
        }

        OrgInfoDto orgInfoDto = indexMapper.getOrgInfo(Integer.valueOf(orgId));
        orgInfoDto.setWasteList(indexMapper.getOrgWasteListByOrgId(Integer.valueOf(orgId)));

        return ResponseParams.ok(orgInfoDto);
    }

    public ResponseParams<?> getWasteInfo() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        String wasteId = request.getParameter("wasteId");

        if (StringUtils.isEmpty(wasteId)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("wasteId"));
        }

        OrgWaste orgWaste = orgWasteMapper.selectById(Integer.valueOf(wasteId));
        OrgWasteInfoDto orgWasteInfoDto = new OrgWasteInfoDto();
        if (orgWaste != null) {
            BeanUtils.copyProperties(orgWaste, orgWasteInfoDto);
        }


        return ResponseParams.ok(orgWasteInfoDto);
    }

    public ResponseParams<?> search() throws Exception {

        String type = request.getParameter("type");
        String searchStr = request.getParameter("searchStr");

        if (StringUtils.isEmpty(type)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("type"));
        }
        if (StringUtils.isEmpty(searchStr)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("searchStr"));
        }
        if(request.getParameter("token").length()>12){
            Integer memberId = ss.getCurrentUserId();

            //将搜索关键字冗余到搜索表中
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.setKeyWord(searchStr);
            searchHistory.setMemberId(memberId);
            searchHistory.setCreateDate(new Date());
            searchHistoryMapper.insert(searchHistory);
            if (type.equals("0")) {
                //搜索资讯
                List<CmsPostListDto> cmsPostList = indexMapper.searchPostByContent(searchStr);
                return ResponseParams.ok(cmsPostList);
            } else if (type.equals("1")) {
                //搜索经营单位
                List<OrgListDto> orgList = indexMapper.searchOrgByName(searchStr);
                if (orgList != null) {
                    for (OrgListDto o : orgList) {
                        o.setWasteList(indexMapper.getOrgWasteByOrgId(o.getOrgId()));
                    }
                }
                return ResponseParams.ok(orgList);
            }
        }else{
            if (type.equals("0")) {
                //搜索资讯
                List<CmsPostListDto> cmsPostList = indexMapper.searchPostByContent(searchStr);
                return ResponseParams.ok(cmsPostList);
            } else if (type.equals("1")) {
                //搜索经营单位
                List<OrgListDto> orgList = indexMapper.searchOrgByName(searchStr);
                if (orgList != null) {
                    for (OrgListDto o : orgList) {
                        o.setWasteList(indexMapper.getOrgWasteByOrgId(o.getOrgId()));
                    }
                }
                return ResponseParams.ok(orgList);
            }
        }


        return ResponseParams.ok(null);
    }


    public ResponseParams<?> getSearchKeyList() throws Exception {

        Map<String, Object> resultMap = new HashMap<>();
        if(request.getParameter("token").length()>12){
            Integer memberId = ss.getCurrentUserId();
            if(memberId!=null){
                resultMap.put("myList", searchHistoryMapper.getMySearchList(memberId));
                resultMap.put("hotList", searchHistoryMapper.getHotSearchList(memberId));
            }
        }else {
            resultMap.put("hotList", searchHistoryMapper.getHotSearchList());
        }


        return ResponseParams.ok(resultMap);
    }

    public ResponseParams<?> deleteSearch() throws Exception{
        Integer memberId = ss.getCurrentUserId();
        searchHistoryMapper.deleteSearch(memberId);
        return ResponseParams.ok(null);
    }

}
